#_*_coding:utf-8_*_
#Create time:2016-08-08

import sys,os,shutil,ConfigParser

def ListProj():
    pkgPath = cf.get('pkgLocation', 'path')
    pkgPath =  os.path.join(pkgPath,os.listdir(pkgPath)[0])
    global projList
    projList = os.listdir(pkgPath)
    while True:
        print "This project will be update %s" %(projList)
        rst = raw_input("Do you want to update?[Yes/no]:").strip()
        if rst == 'no':
            exit(1)
        elif rst == 'Yes':
            break
        else:
            continue
    for i in projList:
        projName = cf.get(i,'appname')
        svnPath = cf.get('svnInfo','svnPath')
        projType = cf.get(i,'type')
        if 'cdn' in os.listdir(os.path.join(pkgPath,i)):
            cpCdn(pkgPath,i,svnPath,projName,projType)
    projList = os.listdir(pkgPath)

def cpCdn(pkgPath,projSvnName,svnPath,projName,projType):
    cf = ConfigParser.ConfigParser()
    cf_file = "cfg.conf"
    cf.read(cf_file)
    dstDir = os.path.join(pkgPath,'res','res_'+projName)
    print "Copying %s to cdn project..." %(projSvnName)
    projCdn = os.path.join(pkgPath,projSvnName,'cdn')
    if os.listdir(projCdn):
        if os.path.exists(dstDir):
            print "Dest path '%s' exists, deleting...." %(dstDir),
            shutil.rmtree(dstDir)
            print "Done!"
        shutil.copytree(projCdn,dstDir)
        #print projCdn,dstDir
    print "Done!"
    #cpSvn(projType,pkgPath,projName,svnPath,cf.get(projSvnName,'appname'))
    
def cpSvn():
    pkgPath = cf.get('pkgLocation', 'path')
    pkgPath =  os.path.join(pkgPath,os.listdir(pkgPath)[0])
    for i in projList:
        upFile=[]
        for fpath,dirs,fs in os.walk(os.path.join(pkgPath,i)):
            for f in fs:
                f =  os.path.join(fpath,f)
                projName = cf.get(i,'appname')
                projType = cf.get(i,'type')
                svnPath = cf.get('svnInfo','svnPath')
                relaPath =  f[len(os.path.join(pkgPath,i))+1:]
                dPath = os.path.join(svnPath,projType + '_' + projName,relaPath)
                if not os.path.exists(os.path.dirname(dPath)):
                    os.makedirs(os.path.dirname(dPath))
                #print f,dPath
                shutil.copy(f,dPath)
                upFile.append(dPath)
        Update(i,upFile)
    
def Update(i,upFile):
    svnCmd = cf.get('svnInfo','cmdPath')
    svnHost = cf.get('svnInfo','host')
    svnUser = cf.get('svnInfo','user')
    svnPass = cf.get('svnInfo','passwd')
    svnPath = cf.get('svnInfo','svnPath')
    projType = cf.get(i,'type')
    projName = cf.get(i,'appname')
    projSvnName = projType + '_' + projName
    os.chdir(os.path.join(svnPath,projSvnName))
    os.system("%s update" %(svnCmd) )
    for i in upFile:
        os.system("%s add %s " %(svnCmd,i) )
    os.system("%s commit --username %s --password %s -m reboot" %(svnCmd,svnUser,svnPass) )
    
if __name__ == '__main__':
    cf = ConfigParser.ConfigParser()
    cf_file = "cfg.conf"
    cf.read(cf_file)
    ListProj()
    cpSvn()
